package com.situ.day12;

import com.mysql.cj.util.StringInspector;
import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JDBCDemo {

    @Test
    public void test(){
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            //1、加载驱动Class.forName("");
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2、获得连接对象Connection
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/study?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8", "root", "123456");
            //3、写sql语句
            String sql = "select id,name,age,gender from student";
            //4、创建Statement(一艘船)
            statement = connection.createStatement();
            //5、执行sql语句
            //(1) 更新类（更改了表里面数据）：delete/update/insert   executeUpdate()
            //返回值：int，表示你影响的行数
            //(2)查询（没有改变表里面数据）:  select                 executeQuery()
            //返回值：结果集ResultSet
            resultSet = statement.executeQuery(sql);
            List<Student> list = new ArrayList<>();
            while (resultSet.next()) {//判断下一个有没有，如果没有返回false，如果有返回true，并且指向这一行
                //当前resultSet指向第一行
                //while循环每遍历一次，把这一行每个字段的值拿出来，封装成一个Student对象
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
            //6、关闭连接
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Test
    public void test2(){
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/study?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8", "root", "123456");
            String sql = "select id,name,age,gender from student";
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            List<Student> list = new ArrayList<>();
            while (resultSet.next()) {//判断下一个有没有，如果没有返回false，如果有返回true，并且指向这一行
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
    }

    @Test
    public void test3(){ //MyBatis
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,age,gender from student";
            //预编译
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            List<Student> list = new ArrayList<>();
            while (resultSet.next()) {//判断下一个有没有，如果没有返回false，如果有返回true，并且指向这一行
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        }  catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
    }


    //增
    @Test
    public void testInsert(){
        Connection connection = null;
        PreparedStatement statement = null;
        try{
            connection = JDBCUtil.getConnection();
            //String name = "zhansgan";
            //String sql = "insert into student(name, age,gender) values("+name+",?,?)"
            //?是占位符
            String sql = "insert into student(name,age,gender) values(?,?,?)";
            statement =connection.prepareStatement(sql);
            statement.setString(1,"张三");
            statement.setInt(2,23);
            statement.setString(3,"男");
            int count =statement.executeUpdate();
            System.out.println("count:" + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection,statement,null);
        }
    }


    //删
    @Test
    public void testDelete(){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from student where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,4);
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count" + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection,statement,null);
        }
    }

    //改
    @Test
    public void testUpdate(){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update student set name=?,age=?,gender=? where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,"王五1");
            statement.setInt(2,24);
            statement.setString(3,"女");
            statement.setInt(4,3);
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count" + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection,statement,null);
        }
    }

    //查
    @Test
    public void restSelectLike(){
        Connection connection = null;
        PreparedStatement statement =null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT  id,name,age,gender FROM student WHERE name LIKE ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,"%王%");
            System.out.println(statement);
            resultSet = statement.executeQuery();
            List<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String  gender = resultSet.getString("gender");
                Student student = new Student(id,name,age,gender);
                list.add(student);
            }
            for (Student student : list){
                System.out.println(student);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection,statement,null);
        }
    }
}

